using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;

public partial class Admin_ThemSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
        }
    }

    private void LoadDropDownList()
    {
        tbLoaiSanPhamEntities.tbLoaiSanPhamEntitiesCollection LoaiSPCollection = tbLoaiSanPhamBUS.GetAll();
        ddl_LoaiSP.DataTextField = tbLoaiSanPhamEntities.tbLoaiSanPhanEntitiesColumns.TenLoai.ToString();
        ddl_LoaiSP.DataValueField = tbLoaiSanPhamEntities.tbLoaiSanPhanEntitiesColumns.MaLoai.ToString();
        ddl_LoaiSP.DataSource = LoaiSPCollection;
        ddl_LoaiSP.DataBind(); 
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {        
        
            tbSanPhamEntities sanPham = new tbSanPhamEntities();
            sanPham.TenSP = txt_TenSP.Text;           
            sanPham.MoTa = ftb_MoTa.Text;
            sanPham.MaLoai = int.Parse(ddl_LoaiSP.SelectedValue);
            int temp = 1;
            int temp1 = 0;
            sanPham.MaNV = temp;
            sanPham.MaDN = temp;
            sanPham.SLBan = temp1;
            sanPham.SLCon = int.Parse(txt_SoLuong.Text);            
            sanPham.GiaGoc = int.Parse(txt_giagoc.Text);
            sanPham.GiaBan = int.Parse(txt_giaban.Text);
            sanPham.NgayCapNhat = DateTime.Now.Date;
            sanPham.MaTT = temp;  
              
            int id = tbSanPhamBUS.Insert(sanPham);

            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    //hpf.SaveAs(Server.MapPath("Photos") + "_" + Path.GetFileName(hpf.FileName));
                    string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                    hpf.SaveAs(Server.MapPath("~/Photos/" + filename));

                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Photos/" + filename));
                    ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);

                    tbHinhAnhEntities ha = new tbHinhAnhEntities();
                    ha.MaSP = id;
                    ha.HinhAnh = filename;                   
                    tbHinhAnhBUS.Insert(ha);

                }
            }
  
           Response.Redirect("~/Admin/DanhSachSanPham.aspx");      
     

    }
}
